/*
 * igo: com.dota.framework.struts2.BooleanCheckboxConverter.java Create On 2011-5-4 下午08:29:06 By Q-Wang
 * $Revision: 1.1 $
 */
package com.ctvit.framework.struts2;

import java.lang.reflect.Member;
import java.util.Map;

import com.opensymphony.xwork2.conversion.TypeConverter;

/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
public class BooleanCheckboxConverter implements TypeConverter {

	/**
	 * @see com.opensymphony.xwork2.conversion.TypeConverter#convertValue(java.util.Map, java.lang.Object, java.lang.reflect.Member, java.lang.String, java.lang.Object, java.lang.Class)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
		if (toType.equals(String.class)) {
			if (value==null||value.equals("")) {
				return "N";
			}
			if (((String[])value)[0].equalsIgnoreCase("true")) {
				return "Y";
			}
			return "N";
		}
		if (toType.equals(Boolean.class)||toType.equals(boolean.class)) {
			if (value==null||value.equals("")) {
				return Boolean.FALSE;
			}
			if (((String[])value)[0].equalsIgnoreCase("true")) {
				return Boolean.TRUE;
			}
			return Boolean.FALSE;
		}
		if (Number.class.isAssignableFrom(toType)) {
			if (value==null||value.equals("")) {
				return 0;
			}
			String v = ((String[])value)[0];
			if (v.equalsIgnoreCase("true")) {
				return 1;
			}
			return 0;
		}
		throw new IllegalStateException();
	}

}
